package com.tedu.services;

import com.alibaba.druid.util.StringUtils;
import com.tedu.mapper.SmbmsUserMapper;
import com.tedu.pojo.SmbmsUser;
import com.tedu.pojo.SmbmsUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务层接口实现类  交给spring管理
 */

@Service  //该组件是服务层的实现类并且交给了spring管理
public class UserServicesImpl implements UserServices{
    //完成了spring整合mybatis
    //spring容器中已经有mapper实现类对象
    //从spring容器中取出mapper对象

    @Autowired
    SmbmsUserMapper userMapper;

    //登录
    @Override
    public SmbmsUser getUserByLogin(SmbmsUser u) {

        //使用mybatis获取数据库的数据
        //设置查询条件
        SmbmsUserExample example = new SmbmsUserExample();
        SmbmsUserExample.Criteria criteria = example.createCriteria();
        //用户名相同
        criteria.andUsercodeEqualTo(u.getUsercode());
        //密码相同
        criteria.andUserpasswordEqualTo(u.getUserpassword());

        List<SmbmsUser> smbmsUsers = userMapper.selectByExample(example);
        if (smbmsUsers.size()>0){
            //登录成功
            return smbmsUsers.get(0);
        }else{
            //登录失败
            return null;
        }

    }

    @Override
    public List<SmbmsUser> getAllUser() {
        return userMapper.selectByExample(new SmbmsUserExample());
    }

    @Override
    public boolean modifyPasswd(String userCode, String userPassword, String newPassword) {
        boolean flag=false;
        String msg="";
        try {
            if(!StringUtils.isEmpty(userCode)&& null!=userPassword&&null!=newPassword) {
                if(userMapper.modifyPasswd(userCode, userPassword,newPassword)>0) {
                    flag=true;
                }else
                {
                    msg="修改失败";
                }
            }else
                msg="参数为空错误！";
        }catch(RuntimeException e) {
            throw new RuntimeException(msg+e);
        }
        return flag;
    }

    @Override
    public SmbmsUser getUserById(int uid) {
        return userMapper.selectByPrimaryKey(new Long(uid));
    }
}
